window.CopyTheCodeToClipboard = (function (window, document, navigator) {
var textArea,
copy;
function isOS() {
return navigator.userAgent.match(/ipad|iphone/i);
}
function createTextArea(text) {
textArea = document.createElement('textArea');
textArea.value = text;
document.body.appendChild(textArea);
}
function selectText() {
var range,
selection;
if (isOS()) {
range = document.createRange();
range.selectNodeContents(textArea);
selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
textArea.setSelectionRange(0, 999999);
} else {
textArea.select();
}
}
function copyToClipboard() {
document.execCommand('copy');
document.body.removeChild(textArea);
// Redirect to page.
if( copyTheCode.redirect_url ) {
window.location.href = copyTheCode.redirect_url;
}
}
copy = function (text) {
createTextArea(text);
selectText();
copyToClipboard();
};
return {
copy: copy
};
})(window, document, navigator);
(function ($) {
CopyTheCode = {
selector: copyTheCode.settings.selector || copyTheCode.selector || 'pre',
button_position: copyTheCode.settings['button-position'] || 'inside',
/**
* Init
*/
init: function () {
this._bind();
this._initialize();
},
/**
* Binds events
*/
_bind: function () {
$(document).on('click', '.copy-the-code-button', CopyTheCode.copyCode);
$(document).on('click', '.copy-the-code-shortcode', CopyTheCode.copyShortcode);
},
/**
* Initialize the Button
*/
_initialize: function () {
if (!$(copyTheCode.selectors).length) {
return;
}
$(copyTheCode.selectors).each(function (index, el) {
var button_copy_text = el['button_copy_text'] || '';
var button_position = el['button_position'] || '';
var button_text = el['button_text'] || '';
var button_title = el['button_title'] || '';
var selector = el['selector'] || '';
var style = el['style'] || '';
var copy_format = el['copy_format'] || '';
$(selector).each(function (index, single_selector) {
var buttonMarkup = CopyTheCode._getButtonMarkup(button_title, button_text, style);
$(single_selector).addClass('copy-the-code-target');
if ('cover' !== style && 'outside' === button_position) {
$(single_selector).wrap('');
$(single_selector).parent().prepend('
' + buttonMarkup + '
');
} else {
$(single_selector).wrap('');
$(single_selector).append(buttonMarkup);
}
switch (style) {
case 'svg-icon':
$(single_selector).find('.copy-the-code-button').html(copyTheCode.buttonSvg);
break;
case 'cover':
case 'button':
default:
$(single_selector).find('.copy-the-code-button').html(button_text);
break;
}
});
});
},
/**
* Get Copy Button Markup
*/
_getButtonMarkup: function (button_title, button_text, style) {
if ('svg-icon' === style) {
button_text = copyTheCode.buttonSvg;
}
return '';
},
format: function (html) {
var tab = '\t';
var result = '';
var indent = '';
html.split(/>\s*).forEach(function (element) {
if (element.match(/^\/\w/)) {
indent = indent.substring(tab.length);
}
result += indent + '<' + element + '>\r\n';
if (element.match(/^\w[^>]*[^\/]$/) && !element.startsWith("input")) {
indent += tab;
}
});
return result.substring(1, result.length - 3);
},
/**
* Copy to Clipboard
*/
copyShortcode: function (event) {
event.preventDefault();
var btn = $(this),
oldText = btn.text(),
target = btn.attr('data-target') || '',
copy_content_as = btn.attr('data-copy-as') || copyTheCode.copy_content_as,
button_copy_text = btn.attr('data-button-copy-text') || '',
content = btn.attr('data-content') || '',
link = btn.attr( 'data-link' ) || '';
// Copy the secrate content.
if (content) {
CopyTheCodeToClipboard.copy(content);
// Copied!
btn.text(button_copy_text);
setTimeout(function () {
btn.text(oldText);
if( link ) {
window.open(link, '_blank').focus()
}
}, 1000);
return;
}
var source = $(target);
if (!source.length) {
btn.text('Not found!');
setTimeout(function () {
btn.text(oldText);
}, 1000);
return;
}
var html = source.html();
html = CopyTheCode.format(html);
if ('html' !== copy_content_as) {
// Convert the tags into new line.
var brRegex = / /gi;
html = html.replace(brRegex, "\n");
// Convert the
tags into new line.
var divRegex = /
/gi;
html = html.replace(divRegex, "\n");
// Convert the
tags into new line.
var pRegex = /
/gi;
html = html.replace(pRegex, "\n");
// Convert the
tags into new line.
var pRegex = /
/gi;
html = html.replace(pRegex, "\n");
// Remove all tags.
html = html.replace(/(<([^>]+)>)/ig, '');
}
if ('html' !== copy_content_as) {
html = html.replace(/[\t\n]+/gm, ' ').trim();
} else {
var reWhiteSpace = new RegExp("/^\s+$/");
html = html.replace(reWhiteSpace, "");
}
var tempElement = $("");
$("body").append(tempElement);
html = $.trim(html);
$('#temp-element').html(html);
var html = $('#temp-element').html();
$('#temp-element').remove();
var tempHTML = html;
// Copy the Code.
var tempPre = $("